/**************************************************************
 *@ProjectName: blweibo
 *@FileName: PopupServiceHelper.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.AsyncTask;

import com.zhiweibo.util.HttpUtil;
import com.zhiweibo.util.Logger;

/**
 * The Class PopupServiceHelper.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
class PopupServiceHelper {

    /** The context. */
    private final Context context;

    /** The m notification manager. */
    private final NotificationManager mNotificationManager;

    /**
     * Instantiates a new popup service helper.
     * 
     * @param paramContext
     *            the param context
     */
    PopupServiceHelper(Context paramContext) {
        context = paramContext.getApplicationContext();
        mNotificationManager = (NotificationManager) paramContext.getSystemService("notification");
    }

    /**
     * Download new package.
     * 
     * @param paramString
     *            the param string
     */
    private void downloadNewPackage(String paramString) {
        Uri localUri1 = Uri.parse(paramString);
        if (localUri1 != null) {
            Intent localIntent = new Intent("android.intent.action.VIEW", localUri1);
            PendingIntent localPendingIntent = PendingIntent.getActivity(context, -1, localIntent, 268435456);
            Notification localNotification = new Notification();
            localNotification.icon = R.drawable.logo;
            int i = localNotification.flags | 0x10;
            localNotification.flags = i;
            localNotification.defaults = 4;
            localNotification.sound = Uri.parse("android.resource://com.hbr/raw/notificationsound");
            localNotification.vibrate = new long[] { 80L, 80L };
            String str1 = context.getString(R.string.app_name);
            String str2 = context.getString(R.string.download_new_version_or_not);
            localNotification.setLatestEventInfo(context, str1, str2, localPendingIntent);
            mNotificationManager.notify(101000, localNotification);
        }
    }

    /**
     * Post notification new version.
     * 
     * @param paramString1
     *            the param string1
     * @param paramString2
     *            the param string2
     */
    private void postNotificationNewVersion(String paramString1, final String paramString2) {
        try {
            String str1 = getVersion(context);
            Matcher localMatcher = Pattern.compile("(\\d+)\\.(\\d)\\.(\\d)").matcher(str1);
            if (localMatcher.matches()) {
                String str5 = localMatcher.group(1) + "00" + localMatcher.group(2) + localMatcher.group(3);
                if (str5.compareTo(paramString1) < 0) {
                    downloadNewPackage(paramString2);
                }
            }
        } catch (NameNotFoundException e) {
            Logger.d(e);
        }
    }

    /**
     * Post notification new version.
     */
    void postNotificationNewVersion() {
        (new AsyncTask<Object, Integer, String>() {
            @Override
            protected String doInBackground(Object... paramArrayOfVoid) {
                return HttpUtil.getVersion();
            }

            @Override
            protected void onPostExecute(String strVersion) {
                postNotificationNewVersion(strVersion, "http://t.hrbanlv.com/download/android.apk");
            }
        }).execute(0);
    }

    /**
     * Gets the version.
     * 
     * @param paramContext
     *            the param context
     * @return the version
     * @throws NameNotFoundException
     *             the name not found exception
     */
    public static String getVersion(Context paramContext) throws PackageManager.NameNotFoundException {
        PackageManager localPackageManager = paramContext.getPackageManager();
        String str1 = paramContext.getPackageName();
        PackageInfo localPackageInfo = localPackageManager.getPackageInfo(str1, 64);
        if (localPackageInfo == null) {
            return null;
        } else {
            return localPackageInfo.versionName;
        }
    }
}